package com.thesis.manage.common.controller;

import com.github.pagehelper.PageInfo;
import com.thesis.manage.common.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author php
 * @date 2018/1/16
 */
@Slf4j
public class AbstractController {


    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    protected String getToken() {
        String token = getRequest().getHeader("token");
        if (StringUtils.isBlank(token)) {
            throw new CommonException("token.get.failed", "token获取失败");
        }
        return token;
    }

    protected Map<String, Object> buildMessage(IExecute execute) {
        HashMap<String, Object> map = new HashMap<>(4);
        try {
            Object e = execute.getData();
            if (e != null) {
                boolean seted = false;
                HashMap<String, Object> dataItem;
                if (e instanceof List) {
                    dataItem = new HashMap<>(1);
                    dataItem.put("list", e);
                    map.put("data", dataItem);
                    map.put("success", Boolean.TRUE);
                    return map;
                }
                if (e instanceof PageInfo) {
                    seted = true;
                    dataItem = new HashMap<>(5);
                    PageInfo page = (PageInfo) e;
                    dataItem.put("list", page.getList());
                    dataItem.put("pageSize", page.getPageSize());
                    dataItem.put("totalCount", page.getTotal());
                    dataItem.put("totalPage", page.getPages());
                    dataItem.put("pageIndex", page.getPageNum());
                    map.put("data", dataItem);
                }
                if (!seted) {
                    map.put("data", e);
                }
            }
            map.put("success", Boolean.TRUE);
        } catch (CommonException var13) {
            log.error(var13.getMessage(), var13);
            map.put("code", HttpStatus.BAD_GATEWAY.value());
            map.put("message", var13.getValue());
            map.put("success", Boolean.FALSE);
        } catch (IllegalArgumentException e) {
            log.error(e.getMessage(), e);
            map.put("code", HttpStatus.BAD_REQUEST.value());
            map.put("message", e.getMessage());
            map.put("success", Boolean.FALSE);
        } catch (Exception var14) {
            log.error(var14.getMessage(), var14);
            map.put("code", HttpStatus.BAD_GATEWAY.value());
            map.put("message", "执行业务失败");
            map.put("success", Boolean.FALSE);
        }
        return map;
    }
}
